#!/bin/bash
# @Date    : 2018-03-15
# 这个脚本用于生成下载相应平台的区服的SQL备份文件的URL
platform=$1
server_id=$2
endtime=$3
dbstorage_server="183.61.128.204"
sql_base_path=/data/cqbysql
dest_base_path=/data/gamelogs/chuanqi/sql_download
dest_file="${sql_base_path}/${platform}/${endtime}/${platform}-game${server_id}-*-${endtime}.tar.bz2"
http_base_url="http://183.61.128.204:8888/log/sql_download"

runCmd(){
    ssh ${dbstorage_server} -n $1
    return $?
}

checkFileExist(){
    runCmd "ls $1 &> /dev/null"
    if [ $? -ne 0 ];then
        echo "$1不存在"
        exit 1
    fi
}

moveSql(){
    runCmd "cp $1 $2/ &> /dev/null"
    if [ $? -ne 0 ];then
        echo "复制$1 失败"
        exit 1
    fi
}

getFileFullName(){
    full_name=`runCmd "ls $1"`
    echo -n ${full_name}
}

main(){
    checkFileExist ${dest_file}
    checkFileExist ${dest_base_path}
    file_full_name=`getFileFullName ${dest_file}`
    base_file_name=`echo -n ${file_full_name} | awk -F/ '{print $NF}'`
    moveSql ${file_full_name} ${dest_base_path}
    echo -e "下载sql的URL为:\n${http_base_url}/${base_file_name}"
}

case $# in
3)
    main
    # getFileFullName ${dest_file}
    ;;
*)
    echo "usage:`basename $0` platform server_id 备份时间YYYY-MM-DD" 
    ;;
esac